package com.br.unisal.library.view.graphic;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.br.unisal.library.view.LendScreen;
import com.unisal.library.util.Criteria;

public class LendingGraphicView extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private final JLabel lblEmprstimo = new JLabel("Empr\u00E9stimo");
	private final JLabel lblSelecioneOCritrio = new JLabel("Selecione o crit\u00E9rio para sele\u00E7\u00E3o do livro desejado");
	private final JLabel lblAutor = new JLabel("Nome do livro");
	private final JTextField tfNomeDoLivro = new JTextField();
	private final JLabel lblIdentificador = new JLabel("Identificador");
	private final JTextField tfID = new JTextField();
	private final JButton btnVoltar = new JButton("Voltar");
	private final JButton btnBuscar = new JButton("Buscar");
	private LendScreen lendScreen = new LendScreen();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LendingGraphicView frame = new LendingGraphicView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LendingGraphicView() {
		tfID.setColumns(10);
		tfNomeDoLivro.setColumns(10);
		initGUI();
	}
	
	private void initGUI() {
		setTitle("Biblioteca Unisal");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblSelecioneOCritrio)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblAutor)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(tfNomeDoLivro, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup()
											.addGap(8)
											.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
												.addComponent(btnBuscar)
												.addComponent(tfID, 323, 323, 323)))))))
						.addComponent(lblEmprstimo)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblIdentificador))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addComponent(btnVoltar)))
					.addContainerGap(16, Short.MAX_VALUE))
		);
		
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblEmprstimo)
					.addGap(28)
					.addComponent(lblSelecioneOCritrio)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(43)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblAutor)
								.addComponent(tfNomeDoLivro, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(33)
							.addComponent(lblIdentificador))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(84)
							.addComponent(tfID, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnVoltar)
						.addComponent(btnBuscar))
					.addGap(19))
		);
		gl_contentPane.linkSize(SwingConstants.VERTICAL, new Component[] {tfNomeDoLivro, tfID});
		gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {tfNomeDoLivro, tfID});
		contentPane.setLayout(gl_contentPane);
		
		btnVoltar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doBack();
			}
		});
		
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String bookName = tfNomeDoLivro.getText();
				String bookID = tfID.getText();
				doBookSearch(bookName, bookID);
			}
		});
	}

	private void doBookSearch(String bookName, String bookID) {
		Criteria criteria = null;
		String parameter = null;
		if(bookName != null) {
			criteria = Criteria.BOOK_NAME;	
			parameter = bookName;
		} else if(bookID != null) {
			criteria = Criteria.IDENTIFIER;
			parameter = bookID;
		} 
		
		//TODO Error handling
		lendScreen.setSelectionCriteria(criteria);
		lendScreen.setParameter(parameter);	
		lendScreen.doLending();
		
	}

	private void doBack() {
		// TODO Auto-generated method stub
	}
}
